package com.xhwl.centiotdata.suppliersystem.vehicle.nantou;

import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.xhwl.common.dto.cent.vehicle.http.nantou.AccessRecordForInNanTouDTO;
import com.xhwl.common.dto.cent.vehicle.http.nantou.AccessRecordForOutNanTouDTO;
import com.xhwl.common.dto.cent.vehicle.http.nantou.ResponseCommonNanTouDTO;
import com.xhwl.common.dto.cent.vehicle.http.nantou.query.QueryAccessRecordNanTouDTO;
import com.xhwl.common.exception.BusinessException;
import com.xhwl.common.pojo.cent.vehicle.bo.config.VehicleIntegrationConfigBO;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class NanTouHandler {

    private String nanTouResponseHandle(String body) {
        ResponseCommonNanTouDTO responseCommonNanTouDTO = JSON.parseObject(body, ResponseCommonNanTouDTO.class);
        if (responseCommonNanTouDTO.getState() && "200".equals(responseCommonNanTouDTO.getCode())) {
            return responseCommonNanTouDTO.getData();
        } else {
            throw new BusinessException("请求南投车场WEB服务,返回的业务状态码不是200和state不为true");
        }
    }

    public List<AccessRecordForInNanTouDTO> queryAccessRecordForIn(VehicleIntegrationConfigBO vehicleIntegrationConfigBO, QueryAccessRecordNanTouDTO queryAccessRecordNanTouDTO) {
        final String path = "/nan-tou/access-record/in";
        String host = vehicleIntegrationConfigBO.getVehicleParkingInfoBO().getHost();

        String responseBody = HttpUtil.post(host + path, JSON.toJSONString(queryAccessRecordNanTouDTO));

        return JSON.parseArray(nanTouResponseHandle(responseBody), AccessRecordForInNanTouDTO.class);
    }

    public List<AccessRecordForOutNanTouDTO> queryAccessRecordForOut(VehicleIntegrationConfigBO vehicleIntegrationConfigBO, QueryAccessRecordNanTouDTO queryAccessRecordNanTouDTO) {
        final String path = "/nan-tou/access-record/out";

        String host = vehicleIntegrationConfigBO.getVehicleParkingInfoBO().getHost();

        String responseBody = HttpUtil.post(host + path, JSON.toJSONString(queryAccessRecordNanTouDTO));

        return JSON.parseArray(nanTouResponseHandle(responseBody), AccessRecordForOutNanTouDTO.class);
    }

}
